//
//  SelectSearchAimView.m
//  TeamInformatization
//
//  Created by bigger on 2019/4/7.
//  Copyright © 2019年 SPIC. All rights reserved.
//

#import "SelectSearchAimView.h"

@interface SelectSearchAimView () <UITextFieldDelegate>

// 组织范围
@property (nonatomic, strong) UIButton *organizationButton;
// 开始时间
@property (nonatomic, strong) UITextField *startTextField;
// 结束时间
@property (nonatomic, strong) UITextField *endTextField;
//
@property (nonatomic, strong) UIDatePicker *startDatePick;
@property (nonatomic, strong) UIDatePicker *endDatePick;

@end

@implementation SelectSearchAimView

- (instancetype)init
{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        [self creatViews];
    }
    return self;
}

- (void)creatViews
{
    self.organizationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_organizationButton setBackgroundColor:kSetRGBColor(245, 245, 245, 1)];
    [_organizationButton setTitle:@"范围" forState:UIControlStateNormal];
    _organizationButton.titleLabel.font = kFont(PFSCRegular, 13);
    [_organizationButton setTitleColor:kTextAll102Color forState:UIControlStateNormal];
    [_organizationButton setImage:[UIImage imageNamed:@"下拉"] forState:UIControlStateNormal];
    [_organizationButton setImageEdgeInsets:UIEdgeInsetsMake(0, 30, 0.0f, -60)];
    [_organizationButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -10, 0.0f, 10)];
    [_organizationButton addTarget:self action:@selector(selectOrganizationAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_organizationButton];
    [_organizationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15 * kScaleWidth);
        make.centerY.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(120 * kScaleWidth, 40 * kScaleWidth));
    }];
    _organizationButton.layer.masksToBounds = YES;
    _organizationButton.layer.cornerRadius = 20. * kScaleWidth;
    
    NSArray *textArr = @[@"结束时间", @"开始时间"];
    for (int i = 0; i < 2; i++) {
        UITextField *_dataTextField = [[UITextField alloc] init];
        _dataTextField.textAlignment = NSTextAlignmentCenter;
        //    _dataTextField.backgroundColor = [UIColor whiteColor];
        _dataTextField.font = kFont(PFSCRegular, 13);
        _dataTextField.textColor = kTitleTextColor;
        _dataTextField.placeholder = textArr[i];
        _dataTextField.inputAccessoryView = [self setInputAccessoryView];
        if (i == 0) {
            _endTextField = _dataTextField;
            _endDatePick = [self setTextFieldInputData];
            _dataTextField.inputView = _endDatePick;
        } else {
            _startTextField = _dataTextField;
            _startDatePick = [self setTextFieldInputData];
            _dataTextField.inputView = _startDatePick;
        }
        _dataTextField.delegate = self;
        [self addSubview:_dataTextField];
        [_dataTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15 * kScaleWidth - 100 * kScaleWidth * i);
            make.width.mas_equalTo(80 * kScaleWidth);
            make.centerY.mas_equalTo(self);
        }];
    }
    
    SimpleLabel *textLabel = [SimpleLabel initWithText:@"至" TextColor:kTitleTextColor TextFont:kFont(PFSCRegular, 13) TextAlignment:NSTextAlignmentCenter];
    [self addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-95 * kScaleWidth);
        make.width.mas_equalTo(20 * kScaleWidth);
        make.centerY.mas_equalTo(self);
    }];
}

- (UIDatePicker *)setTextFieldInputData
{
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy/MM/dd"];
    
    UIDatePicker *datePick = [[UIDatePicker alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    datePick.locale = locale;
    datePick.maximumDate = [NSDate date];
    datePick.datePickerMode = UIDatePickerModeDate;
    datePick.backgroundColor = [UIColor whiteColor];
    
    return datePick;
    
}

- (UIView *)setInputAccessoryView
{
    UIView *_toolView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    _toolView.backgroundColor = [UIColor whiteColor];
    UIButton * doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(kScreenWidth - 60, 0, 50, 44);
    [doneButton setTitleColor:kTitleTextColor forState:UIControlStateNormal];
    doneButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [doneButton setTitle:@"确定" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_toolView addSubview:doneButton];
    
    UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(10, 0, 50, 44);
    [cancelButton setTitleColor:kTitleTextColor forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_toolView addSubview:cancelButton];
    
    return _toolView;
}


- (void)cancelButtonAction:(UIButton *)sender
{
    [self endEditing:YES];
    
}

- (void)doneButtonAction:(UIButton *)sender
{
    [self endEditing:YES];
}

#pragma mark - TextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
   
    NSDate *selectedDate;
    if (textField == _startTextField) {
       selectedDate = [_startDatePick date];
    } else {
        selectedDate = [_endDatePick date];
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/MM/dd"];
    NSString *destDateString = [dateFormatter stringFromDate:selectedDate];
    
    textField.text = destDateString;
    
}



- (void)selectOrganizationAction:(UIButton *)sender
{
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
